package io.renren.modules.wcs;

import io.renren.common.utils.R;
import io.renren.modules.ioserver.IOServerService;
import io.renren.modules.wcs.enums.IOServerEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/16 16:01
 * @Version 1.0
 */
@Service
public class DDJServiceImpl implements DDJService {
    IOServerService ioServerService = new IOServerService();
    private static final Logger logger = LoggerFactory.getLogger(DDJServiceImpl.class);

    /**
     * 返回
     *      -1 写入IOServer 异常
     *      0  写入IOServer 失败
     *      1 写入IOServer 成功
     **/
    @Override
    public Integer modifyCurrentPLC(DDJArgEntity ddjArgEntity) {
        Short destLayer = Short.valueOf(ddjArgEntity.getDestLayer());
        Short destCol = Short.valueOf(ddjArgEntity.getDestCol());
        Short takePosition = Short.valueOf(ddjArgEntity.getTakePosition());
        Integer result = -1;
        Integer integer = -99;
        Integer integer2 = -99;
        Integer integer3 = -99;
        try {
            integer = ioServerService.setTagValue(IOServerEnum.Current_Layer.getCollectionPointName(), destLayer.toString(), 1);
            integer2 = ioServerService.setTagValue(IOServerEnum.Current_Column.getCollectionPointName(), destCol.toString(), 1);
            integer3 = ioServerService.setTagValue(IOServerEnum.Current_Position.getCollectionPointName(), takePosition.toString(), 1);
        }catch (Exception e){
            return result;
        }
        if(integer ==0 &&integer2 ==0 &&integer3 ==0){
            result = 1;
            return result;
        }else {
            result = 0;
            return result;
        }
    }

    @Override
    public Integer sendTask(DDJArgEntity ddjArgEntity) {
        DDJRunnableCallable2<Integer> task = new DDJRunnableCallable2<>(ddjArgEntity,ioServerService);

        Future<?> future = QGSThreadPool.executor.submit(task);
        Object r = new Object();
        try {
            r = future.get();
        } catch (InterruptedException e){
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return (Integer)r;
    }
}
